/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view;

import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Asset;
import org.braintrain.domain.Module;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: Robert H. Engelhardt
 * Date: 4/19/11
 * Time: 3:45 PM
 */
public abstract class EvaluationReport extends BrainTrainSecuredBasePage implements IExternalPage {

    public abstract Module getModule();
    public abstract void setModule(Module module);

    public abstract Date getStartDate();
    public abstract void setStartDate(Date date);

    public abstract Date getEndDate();
    public abstract void setEndDate(Date date);

    @Asset("script/flot.script")
    public abstract IAsset getFlotScript();

    public void activateExternalPage(Object[] objects, IRequestCycle requestCycle) {

        if(objects == null || objects.length == 0 || !(objects[0] instanceof Long)) {
            throw new ApplicationRuntimeException("EvaluationReport not activated with expected parameters");
        }

        Long moduleId = (Long)objects[0];
        Module module = getGenericService().getById(Module.class, moduleId);

        if(module == null) {
            throw new ApplicationRuntimeException("Unknown module with id of " + moduleId);
        }

        setModule(module);

        setStartDate(new Date(0));
        setEndDate(new Date());

    }

    public void doSubmit(IRequestCycle cycle) {

        if(getStartDate()==null) {
            setStartDate(new Date(0));
        }

        if(getEndDate()==null) {
            setEndDate(new Date());
        }

    }

    public Format getDateFormat() {
        return new SimpleDateFormat("MM/dd/yyyy");
    }

}
